<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<!-- 通过config接口注入权限验证配置 -->
<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>

<!-- 通过agentConfig注入应用的权限 -->
<script src="https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js"></script>

<script src="https://wwcdn.weixin.qq.com/node/open/js/wecom-jssdk-2.0.2.js"></script>

<head>
    <title>测试获取用户ID</title>
    <meta charset="UTF-8">
    <style>
    </style>
</head>
<body>
    <div class="container">
        <h2>INFO:</h2>
        <h2 id="info"></h2>
        <h2 id="userId">外部联系人ID：</h2>
        <button onclick="sendChatMessage">分享消息到当前会话</button>
    </div>
    
    <script th:inline="javascript">
        const config = {
            appId: /*[[${config.appId}]]*/ '',
            timestamp: /*[[${config.timestamp}]]*/ '',
            nonceStr: /*[[${config.nonceStr}]]*/ '',
            signature: /*[[${config.signature}]]*/ ''
        };
        
        // 获取当前会话的ExternalUserId
        //setTimeout(checkWxReady, 500); 
        
        
        setTimeout(getContext, 500);
        
        // 方式一：使用 window.onload
        function checkWxReady(){
            document.getElementById('info').innerHTML = "服务器返回config：" + JSON.stringify(config);
            
            document.getElementById('info').innerHTML = "执行wx.agentConfig：" + wx.agentConfig;
            
            wx.agentConfig({
                corpid: config.appId,
                agentid: '1000008',
                timestamp: config.timestamp,
                nonceStr: config.nonceStr,
                signature: config.signature,
                jsApiList: ['getCurExternalContact', 'sendChatMessage'],
                success: function(res) {
                    console.info(res);
                    document.getElementById('info').innerHTML = "执行wx.agentConfig：" + JSON.stringify(res);
                    getCurExternalContact();
                },
                fail: function(res) {
                    if(res.errMsg.indexOf('function not exist') > -1){
                        alert('版本过低请升级')
                    }
                    console.error("fail:", res);
                    document.getElementById('info').innerHTML = "执行wx.agentConfig失败：" + JSON.stringify(res);
                }
            });
        }
        
        function getCurExternalContact(){
        	
        	wx.invoke('getCurExternalContact', {
            }, function(res){
    	        if(res.err_msg == "getCurExternalContact:ok"){
    	            console.info("当前外部联系人userId:" + res.userId); //返回当前外部联系人userId
    	            document.getElementById('info').innerHTML = ("外部联系人ID：" + res.userId);
    	        }else {
    	            //错误处理
    	            console.info("获取外部联系人发生异常：" + res);
    	            document.getElementById('info').innerHTML = ("外部联系人ID：发生异常>>" + JSON.stringify(res));
    	        }
    	    });
        }
        
        function getContext(){
        	document.getElementById('info').innerHTML = ("开始执行22getContext...");
        	try{
	        	ww.getContext({
	        		success: function(res){
			            document.getElementById('info').innerHTML = "入口类型：" + res.entry;
	        		},
	        		fail: function(res){
			            document.getElementById('info').innerHTML = "getContext发生错误" + JSON.stringify(res) ;
	        		}
	        		
	        	});
        	} catch (e){
	            document.getElementById('info').innerHTML = ("getContex发生异常：" + e);
        	}
        }
        
        // 分享消息到当前会话
        function sendChatMessage(){
            document.getElementById('info').innerHTML = "分享消息到当前会话：";
	       	wx.sendChatMessage({
	       		  msgtype: 'text',
	       		  text: {
	       		    content: '你好111'
	       		  }
	       		})
        }
    </script>
</body>
</html>